<template>
  <div class="store-container">
    <div class="store-header">
      <div class="store-header-left">
        <div class="store-logo-image" :style="{ backgroundImage: 'url(' + that.$common.getResourceUrl(that.store['storeInformation']['logoImage']) + ')' }"></div>
        <div class="store-summary">
          <h1 class="store-name">{{ store['name'] }}</h1>
          <el-rate :value="store['rank']" disabled show-score text-color="#ff9900" score-template="{value}" style="padding: 10px 0" :colors="['#99A9BF', '#F7BA2A', '#FF9900']"></el-rate>
        </div>
      </div>
      <div class="store-header-right">
        <div class="store-header-content">
          <h3>配送费</h3>
          <p class="store-header-content-txt">{{ store['storeInformation']['deliveryCost'] }}元</p>
        </div>
        <div class="store-header-content store-header-content-border">
          <h3>配送距离</h3>
          <p class="store-header-content-txt">{{ store['storeInformation']['deliveryArea'] }} 公里</p>
        </div>
        <div class="store-header-content store-header-content-border" style="padding-right: 0!important;">
          <h3>商品总数</h3>
          <p class="store-header-content-txt">{{ currentTotal }} 件</p>
        </div>
      </div>
    </div>
    <div class="store-content">
      <!--nav-->
      <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" style="margin: 0 30px;">
        <el-menu-item index="1">所有商品</el-menu-item>
        <el-menu-item index="2">商品评价</el-menu-item>
        <el-menu-item index="3">商家资质</el-menu-item>
      </el-menu>
      <div class="store-product-container">
        <!--products-->
        <div v-if="activeIndex === '1'" class="store-products">
          <el-row :gutter="30">
            <el-col span="12" v-for="product in products" :key="product['id']">
              <div shadow="always" class="product-card">
                <div class="product-card-container">
                  <div class="product-card-image" :style="{ backgroundImage: 'url(' + that.$common.getResourceUrl(product['images']) + ')' }"></div>
                  <div class="product-content">
                    <div class="product-content-summary">
                      <span class="product-name">{{ product['name'] }}</span>
                      <span class="product-price">{{ product['price'] }} 元</span>
                    </div>
                    <span class="product-description">{{ product['description'] }}</span>
                    <div class="product-content-summary">
                      <span class="product-quantity">剩余数量: {{ product['quantity'] }} 份</span>
                      <el-button size="mini" type="primary" @click="addToCart($event, product)">加入购物车</el-button>
                    </div>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
          <!--Pagination-->
          <div class="pagination-container">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPageIndex"
              :page-sizes="[6, 12, 24, 36]"
              :page-size="currentMaxResults"
              layout="total, sizes, prev, pager, next, jumper"
              :total="currentTotal">
            </el-pagination>
          </div>
        </div>
        <div v-if="activeIndex === '2'" class="order-comments">
          <el-table :data="comments" border style="width: 100%">
            <el-table-column prop="createTime" label="评价日期" width="200"></el-table-column>
            <el-table-column prop="message" label="评价" width="400"></el-table-column>
            <el-table-column label="星级" width="200">
              <template slot-scope="scope">
                <el-rate :value="scope.row['rank']" disabled show-score text-color="#ff9900" score-template="{value}"></el-rate>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="activeIndex === '3'" class="store-information">
          <p class="store-information-txt">商家地址: {{ store['address'] }}</p>
          <p class="store-information-txt">商家营业执照编号: {{ store['license'] }}</p>
          <p class="store-information-txt">店铺创建时间: {{ store['createTime'] }}</p>
          <p class="store-information-txt">店铺最后变更时间: {{ store['lastModifiedTime'] }}</p>
        </div>
        <!--description-->
        <div class="store-description-container">
          <p class="store-description-title"><i class="el-icon-bell" style="margin-right: 10px;"></i>商家简介</p>
          <p class="store-description">{{ store['storeInformation']['description'] }}</p>
          <el-button class="store-complain-btn" size="small" @click="showComplainForm">投诉商家</el-button>
        </div>
      </div>
    </div>
    <!--Cart-->
    <ShoppingCart ref="cart"></ShoppingCart>
    <!--Complain Dialog-->
    <el-dialog title="意见反馈" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="cpForm" size="medium">
        <el-form-item label="投诉类型" label-width="120px">
          <el-select v-model="cpForm.type" placeholder="请选择投诉类型" style="width: 90%">
            <el-option label="商家资质问题(商家缺少相关从业资质证书)" value="商家资质问题(商家缺少相关从业资质证书)"></el-option>
            <el-option label="商品价格问题(商家外卖商品价格高于店内实际价格)" value="商品价格问题(商家外卖商品价格高于店内实际价格)"></el-option>
            <el-option label="商品配送问题(商家起送价、配送费或餐盒费过高)" value="商品配送问题(商家起送价、配送费或餐盒费过高)"></el-option>
            <el-option label="其他问题" value="其他问题"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="投诉理由" label-width="120px">
          <el-input type="textarea" v-model="cpForm.reason" style="width: 90%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitComplainForm('cpForm')">确 定</el-button>
      </div>
    </el-dialog>
    <div v-for="item in cartAnis" :key="item">
      <CartItemAnimation :from-x="item.fromX" :from-y="item.fromY" :to-x="item.toX" :to-y="item.toY" @on-finish="finishAni"></CartItemAnimation>
    </div>
  </div>
</template>

<script>
import ShoppingCart from './ShoppingCart'
import CartItemAnimation from './CartItemAnimation'
export default {
  name: 'Store',
  components: {CartItemAnimation, ShoppingCart},
  data () {
    return {
      that: this,
      cartAnis: [],
      activeIndex: '1',
      storeId: this.$route.params['id'],
      store: null,
      products: [],
      currentPageIndex: 1,
      currentMaxResults: 6,
      currentTotal: 0,
      dialogFormVisible: false,
      cpForm: {
        type: '',
        reason: ''
      },
      comments: []
    }
  },
  created () {
    this.$customerAPI.store(this.storeId).then((params) => {
      this.store = params.data
      // initialize shopping cart
      this.$store.state.currentShoppingCart = null
      this.$store.state.shoppingCarts.forEach((currentCart) => {
        if (currentCart.store['id'] === this.store['id']) {
          this.$store.state.currentShoppingCart = currentCart
        }
      })
      if (this.$store.state.currentShoppingCart === null) {
        let cart = new ShoppingCart.Cart(this.store)
        this.$store.state.shoppingCarts.push(cart)
        this.$store.state.currentShoppingCart = cart
      }
    })
    this.initializeStore()
  },
  methods: {
    finishAni () {
      this.cartAnis.splice(0, 1)
    },
    initializeStoreComments () {
      this.$merchantAPI.store_comments(this.storeId).then((params) => {
        this.comments = params.data
      })
    },
    showComplainForm () {
      if (!(this.$store.state.currentUser.id)) {
        this.$message('请先登录！')
        this.$router.push('/customer/login/store')
      } else {
        this.dialogFormVisible = true
      }
    },
    submitComplainForm (formName) {
      this.dialogFormVisible = false
      this.$customerAPI.complain_add({
        'storeId': this.storeId,
        'userId': this.$store.state.currentUser.id,
        'message': this.cpForm.type + ': ' + this.cpForm.reason
      }).then((params) => {
        this.$message({ type: 'success', message: '投诉成功！' })
      })
    },
    resetComplainForm (formName) {
      this.$refs[formName].resetFields()
    },
    initializeStore () {
      this.$customerAPI.store_prod(this.storeId, this.currentPageIndex, this.currentMaxResults).then((params) => {
        this.products = params.data['items']
        this.currentTotal = params.data['itemTotal']
      })
    },
    handleSizeChange (val) {
      this.currentMaxResults = val
      this.initializeStore()
    },
    handleCurrentChange (val) {
      this.currentPageIndex = val
      this.initializeStore()
    },
    handleSelect (index) {
      this.activeIndex = index
      if (index === '2') {
        this.initializeStoreComments()
      }
    },
    addToCart (event, product) {
      console.log()
      this.cartAnis.push({
        fromX: event.clientX,
        fromY: event.clientY,
        toX: this.$refs.cart.$el.offsetLeft,
        toY: this.$refs.cart.$el.offsetTop
      })
      console.log(event.clientX + '  ' + event.clientY)
      this.$store.state.currentShoppingCart.addProduct(product)
    }
  }
}
</script>

<style scoped>
  * {
    transition: all .15s ease-in-out;
  }
  .store-container {
    padding: 0 260px;
    display: flex;
    flex-direction: column;
  }
  .store-header {
    padding: 10px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .store-header-left {
    display: flex;
    align-items: center;
  }
  .store-logo-image {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 3px solid white;
    background-color: cornflowerblue;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
  }
  .store-summary {
    margin-left: 32px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .store-name {
    color: white;
    margin: 8px 0 !important;
  }
  .store-header-content {
    padding: 0 52px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .store-header-right {
    display: flex;
    color: white;
    letter-spacing: 2px;
  }
  .store-header-content-border {
    border-left: 1px solid rgba(255,255,255,0.5);
  }
  .store-header-content-txt {
    margin: 0 !important;
    font-size: 19px;
  }
  .store-content {
    width: 100%;
    margin-top: 26px;
    padding: 6px 0;
    background-color: white;
    border-radius: 5px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    display: flex;
    flex-direction: column;
  }
  .store-product-container {
    display: flex;
    padding: 0 30px;
  }
  .store-products, .order-comments, .store-information {
    width: 80%;
    padding: 30px 30px 30px 0;
  }
  .store-description-container {
    width: 20%;
    margin: 20px 0;
  }
  .store-description-title {
    background-color: #f3b274;
    text-align: center;
    font-size: 18px;
    letter-spacing: 2px;
    color: white;
    margin: 0;
    padding: 8px 0;
  }
  .store-description {
    margin: 0;
    padding: 40px 20px;
    background-color: rgba(0,0,0,0.05);
    font-size: 14px;
    color: #515151;
  }
  .store-complain-btn {
    background-color: rgba(0,0,0,0.25);
    color: white;
    width: 100%;
  }
  .product-card {
    display: flex;
    border-radius: 4px;
    margin-bottom: 30px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    padding: 20px;
    position: relative;
    top: 0;
    cursor: pointer;
  }
  .product-card:hover {
    top: -6px;
  }
  .product-card-image {
    width: 90px;
    height: 90px;
    border-radius: 6px;
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    flex-shrink: 0;
  }
  .product-content {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 20px;
  }
  .product-card-container {
    width: 100%;
    display: flex;
  }
  .product-name, .product-price {
    color: #515151;
    font-size: 17px;
    letter-spacing: 1px;
    line-height: 32px;
    font-weight: bold;
  }
  .product-description, .product-quantity {
    color: #a2a2a2;
    font-size: 13px;
    line-height: 24px;
  }
  .product-content-summary {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .product-price {
    color: #F56C6C !important;
  }
  .pagination-container {
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 32px 0 12px 0;
  }
  .store-information-txt {
    color: #8e8e8e;
    font-size: 15px;
    letter-spacing: 1px;
  }
</style>
